// pages/jinrishouyimore/jinrishouyimore.js
const app = getApp();
// 日期
const date = new Date()
const years = []
const months = []
const days = []

for (let i = 1990; i <= date.getFullYear() + 2; i++) {
  years.push(i)
}

for (let i = 1; i <= 12; i++) {
  months.push(i)
}

for (let i = 1; i <= 31; i++) {
  days.push(i)
}


Page({

  /**
   * 页面的初始数据
   */
  data: {
    select: false, //项目选项
    tihuoWay: '筛选', //默认选项
    list: [],
    yueshouyi: '',
    nowpage: 1,
    totalpage: 1,
    years: years, //日期初始化
    year: '',
    months: months,
    month: '',
    value: [], //选择时间的值【年月日】
    dingdanTimes1: {
      year: '',
      month: ''
    }, //选择后的订单日期
    showflag: false
  },
  //订单详情
  toddxq(e) {
    // console.log(e)
    let type = e.currentTarget.dataset.ordertype
    let dingdanId = e.currentTarget.dataset.dingdanid
    if (type == 1) {
      wx.navigateTo({
        url: '/pages/order/page/chuantongDingdan/dingdanXqwu/dingdanXqwu?dingdanId=' + dingdanId,
      })
    } else if (type == 0){
      wx.navigateTo({
        url: '/pages/order/page/lingshouDingdan/dianpuneiXq/dianpuneiXq?dingdanId=' + dingdanId,
      })
    } else if (type == 2) {
      wx.navigateTo({
        url: '/pages/order/page/lingshouDingdan/dingdanXq/dingdanXq?dingdanId=' + dingdanId,
      })
    }
  },
  // 订单统计日期弹窗弹窗
  dingdanTime: function(e) {
    let value = this.data.value
    value[0] = e.currentTarget.dataset.times.year * 1 - 1990
    value[1] = e.currentTarget.dataset.times.month * 1 - 1
    console.log(value)
    this.setData({
      dingdanModal: true, //订单的时间弹窗
      showAnceng: true, //暗层的显示
      year: e.currentTarget.dataset.times.year * 1,
      month: e.currentTarget.dataset.times.month * 1,
      value: value
    })
  },
  // 关闭订单日期弹窗
  close_dingdanTime: function() {
    this.setData({
      dingdanModal: false, //订单的时间弹窗
      showAnceng: false, //暗层的显示
    })
  },
  // 确定订单日期弹窗
  queding_dingdanTime: function() {
    let that = this
    let dingdanTimes1 = {}
    dingdanTimes1.year = that.data.year
    if (that.data.month * 1 < 10) {
      dingdanTimes1.month = '0' + that.data.month
    } else {
      dingdanTimes1.month = that.data.month
    }
    this.setData({
      dingdanModal: false,
      showAnceng: false,
      dingdanTimes1: dingdanTimes1,
      nowpage: 1,
      totalpage: 1,
      list:[],
      showflag: false
    })
    that.getlist()
  },
  // 订单日期切换
  dingdanChange: function(e) {
    const val = e.detail.value
    this.setData({
      year: this.data.years[val[0]],
      month: this.data.months[val[1]]
    })
  },
  // 弹出下拉
  bindShowMsg() {
    this.setData({
      select: !this.data.select
    })
  },
  // 选择下拉值
  mySelect(e) {
    setTimeout(function () {
      wx.showLoading({
        title: '加载中',
      })
    }, 0)
    var name = e.currentTarget.dataset.name
    this.setData({
      tihuoWay: name,
      select: false,
      nowpage: 1,
      totalpage: 1,
      list: [],
      showflag: false
    })
    this.getlist()
  },
  /**
   * 生命周期函数--监听页面加载
   */
  onLoad: function(options) {
    let that = this
    let dates = new Date()
    let month
    if (dates.getMonth() + 1 < 10) {
      month = '0' + (dates.getMonth() + 1)
    } else {
      month = dates.getMonth() + 1
    }
    that.setData({
      year: dates.getFullYear(),
      month: month,
      dingdanTimes1: {
        year: dates.getFullYear(),
        month: month
      }, //选择后的订单日期
    })
  },

  /**
   * 生命周期函数--监听页面初次渲染完成
   */
  onReady: function() {

  },
  getlist() {
    let that = this
    let state
    if (that.data.tihuoWay == '筛选' || that.data.tihuoWay == '全部'){
      state = ''
    } else if (that.data.tihuoWay == '批发交易收益'){
      state = 0
    } else if (that.data.tihuoWay == '代销零售收益') {
      state = 1
    }
    let start = that.data.dingdanTimes1.year + '-' + that.data.dingdanTimes1.month + '-01 00:00:00'
    let end = that.data.dingdanTimes1.year + '-' + that.data.dingdanTimes1.month + '-31 23:59:59'
    wx.request({
      url: app.globalData.myurl,
      data: {
        cmd: "acctDetailList",
        shopId: wx.getStorageSync('shopId'),
        userId: wx.getStorageSync('userId'),
        type: 0,
        nowPage: that.data.nowpage,
        startTime: start,
        endTime: end,
        state: state
      },
      header: {
        "content-type": "application/x-www-form-urlencoded",
        "token": wx.getStorageSync('token')
      },
      success(res) {
        if (res.data.zjstate == 1) {
          setTimeout(function () {
            wx.hideLoading()
            wx.stopPullDownRefresh() //停止下拉刷新
          }, 0)
          setTimeout(function () {
            wx.showModal({
              title: '您的账号已在其他设备登录，请重新登录！',
              showCancel: false, //是否显示取消按钮false去掉取消按钮
              success: function (res) {
                if (res.confirm) {
                  wx.clearStorageSync()
                  wx.reLaunch({
                    url: '/pages/login/login',
                  })
                } else {
                  wx.clearStorageSync()
                  wx.reLaunch({
                    url: '/pages/login/login',
                  })
                }
              }
            })
          }, 0)
          return
        }
        console.log(res)
        if (res.data.result == 1) {
          setTimeout(function() {
            wx.hideLoading()
          }, 0)
          setTimeout(function() {
            wx.showToast({
              title: res.data.resultNote,
              icon: 'none',
              duration: 1500
            })
          }, 0)
        } else {
          if (that.data.nowpage == 1) {
            let acclist = res.data.accDetailList
            for (var i in acclist) {
              acclist[i].timeheader = acclist[i].orderTime.substring(5, 16)
            }
            that.setData({
              list: acclist,
              totalpage: res.data.totalPage,
              yueshouyi: res.data.income,
              showflag: true
            })
          } else {
            var list = that.data.list;
            let acclist = res.data.accDetailList
            for (var i in acclist) {
              acclist[i].timeheader = acclist[i].orderTime.substring(5, 16)
              list.push(acclist[i])
            }
            that.setData({
              list: list,
              totalpage: res.data.totalPage,
              yueshouyi: res.data.income,
              showflag: true
            })
          }
          setTimeout(function() {
            wx.hideLoading()
          }, 0)
        }
      }
    });
  },
  chudi() {
    let that = this
    let caiwunowpage = that.data.nowpage
    caiwunowpage = caiwunowpage + 1
    let caiwutotalpage = that.data.totalpage
    if (caiwunowpage <= caiwutotalpage) {
      setTimeout(function() {
        wx.showLoading({
          title: '加载中',
        })
      }, 0)
      that.setData({
        nowpage: caiwunowpage
      })
      that.getlist()
    } else {
      wx.showToast({
        title: '已经到底了',
        icon: 'none'
      })
    }
  },
  /**
   * 生命周期函数--监听页面显示
   */
  onShow: function() {
    let that = this
    setTimeout(function() {
      wx.showLoading({
        title: '加载中',
      })
    }, 0)
    that.setData({
      nowpage:1,
      totalpage:1,
      list:[],
      showflag: false
    })
    that.getlist()
  },

  /**
   * 生命周期函数--监听页面隐藏
   */
  onHide: function() {

  },

  /**
   * 生命周期函数--监听页面卸载
   */
  onUnload: function() {

  },

  /**
   * 页面相关事件处理函数--监听用户下拉动作
   */
  onPullDownRefresh: function() {

  },

  /**
   * 页面上拉触底事件的处理函数
   */
  onReachBottom: function() {

  },

  /**
   * 用户点击右上角分享
   */
  onShareAppMessage: function() {
    return {
      title: '众商联采',
      path: "/pages/login/login", // 路径，传递参数到指定页面。
      // imageUrl:'../../../images/logo/LOGO.png'
      imageUrl: app.globalData.logo
    }
  }
})